package com.example.exam.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author zhozixiong
 * @date 2022/7/19
 * <p>
 * json工具类
 */
@Slf4j
public class JsonUtil {

    public static final ObjectMapper mapper = new ObjectMapper();

    private JsonUtil() {
    }

    /**
     * 对象转json字符串
     */
    public static String toString(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj.getClass() == String.class) {
            return (String) obj;
        }
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("json序列化出错:" + obj, e);
            return null;
        }
    }

    /**
     * json字符串转对象
     *
     * @param json
     * @param tClass
     * @return
     */
    public static <T> T toBean(String json, Class<T> tClass) {
        try {
            return mapper.readValue(json, tClass);
        } catch (IOException e) {
            log.error("json解析出错:" + json, e);
            return null;
        }
    }

    /**
     * json字符串转List集合
     *
     * @param json   json字符串
     * @param eClass List存储的类型
     * @return List集合
     */
    public static <E> List<E> toList(String json, Class<E> eClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
        } catch (IOException e) {
            log.error("json解析出错:" + json, e);
            return null;
        }
    }

    /**
     * json字符串转Map集合
     *
     * @param json   json字符串
     * @param kClass map key的类型
     * @param vClass map value的类型
     * @return map集合
     */
    public static <K, V> Map<K, V> toMap(String json, Class<K> kClass, Class<V> vClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            log.error("json解析出错:" + json, e);
            return null;
        }
    }

    /**
     * 泛型转泛型
     *
     * @param json json字符串
     * @param type 要转化成的类型
     * @return 相应的类型
     */
    public static <T> T parseObject(String json, Class<T> type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            log.error("json解析出错:" + json, e);
            return null;
        }
    }

    public static <T> T parseObject(InputStream json, Class<T> type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            log.error("json解析出错:" + json, e);
            return null;
        }
    }

    public static String writeValueAsString(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("writeValueAsString:", e);
            return "";
        }
    }
}
